<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/11/2
 * Time: 14:43
 */

namespace app\api\model;


class CardProduct extends BaseModel
{
    protected $autoWriteTimestamp = true;
    protected $hidden=['id','create_time','update_time'];

    /**
     * 与product是多对一的关系
     * @return \think\model\relation\BelongsToMany
     */
    public function goods(){
        return $this->belongsToMany('Product','card_product','p_id','id');
    }
    /**
     * 获取推荐商品
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getGoods($wu_id){
        return self::with(['goods'])->where('w_id','=',$wu_id)->select();
    }
    /**
     * 添加推荐商品
     * @param $info
     * @param $wu_id
     * @return false|int
     */
    public function  addGoods($info,$wu_id){
        $status = self::where('w_id','=',$wu_id)->delete();
        if ($info != null) {
            foreach ($info as $key => $value) {
                $data[$key]['w_id'] = $wu_id;
                $data[$key]['p_id'] = $value['p_id'];
            }return self::saveAll($data);
        }
        return $status;
    }

    /**
     * 获取用户ID
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getUserByID($id){
        $status = self::where('p_id','=',$id)->field('w_id')->select();
        return $status;
    }

    /**
     * 删除用户ID
     * @param $pid
     * @return int
     */
    public static function deleteByPid($pid){
            return self::where('p_id','=',$pid)->delete();
    }

}